<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 4);

include_once("validate.php");

if(!isset($_GET['lastevent']))
{
	die('Must have variable lastevent set');
}

$champ_weight = 40;
$champ_count = 8;
$event_weight = 20;
$event_count = 6;
$new_weight = 20;


$event = $_GET['lastevent'];

function calculateWeight($div, $pts, $mode, $max)
{
	if($mode == 'champ')
	{
		return (($pts - $div['champmin']) / ($div['champmax'] - $div['champmin'])) * $max;
	}
	else
	{
		return (($pts - $div['eventmin']) / ($div['eventmax'] - $div['eventmin'])) * $max;
	}
}

function compare_drivers($a, $b)
{
	return $b->getWeight() - $a->getWeight();
}

class driver
{
	var $id, $drivername;
	var $weight = 0;

	function driver($id, $drivername)
	{
		$this->id = $id;
		$this->drivername = $drivername;
	}
	
	function addWeight($add)
	{
		$this->weight = $this->weight + $add;
	}
	
	function getWeight()
	{
		return round($this->weight);
	}
	
	function getOutput()
	{
		return '/setmass ' . round($this->weight) . ' ' . $this->drivername;
	}
}


$drivers = Array();
$division = Array();

$division['id'] = $_GET['division'];;

$q = 'SELECT (SELECT SUM(r_points) FROM lc_raceresults WHERE r_driver = u.user_id AND r_event = '.$event.') as points
			FROM lc_champtable_drivers, bb_users u, bb_profile_fields_data d
			WHERE u.user_id = d.user_id AND u.user_id = ct_id AND ct_div = ' . $division['id'] . '
			ORDER BY points DESC
			LIMIT ' . $event_count . ', 1';

$res = $db->sql_query($q);
if ($row = $db->sql_fetchrow($res))
{
	$division['eventmin'] = $row['points'];
}
else
{
	die('Error getting event points for Division id ' . $division['id']);
}

$q = 'SELECT ct_points + ct_penalty  AS points
		FROM lc_champtable_drivers 
		WHERE ct_div = ' . $division['id'] . '
		ORDER BY points DESC
		LIMIT ' . $champ_count . ', 1';

$res = $db->sql_query($q);
if ($row = $db->sql_fetchrow($res))
{
	$division['champmin'] = $row['points'];
}
else
{
	die('Error getting champ points for Division '. $division['id'] );
}


$q = 'SELECT (ct_points + ct_penalty) AS points, pf_drivername as user_drivername, u.user_id
		FROM lc_champtable_drivers, bb_users u, bb_profile_fields_data d
		WHERE u.user_id = d.user_id AND u.user_id = ct_id AND ct_div = ' . $division['id'] . '
		ORDER BY points DESC
		LIMIT 0, ' . $champ_count;
$res = $db->sql_query($q);
while ($row = $db->sql_fetchrow($res))
{
	echo $row['user_drivername'] . ' ' . $row['points'] . '<br>';
	if(!isset($division['champmax']))
	{
		$division['champmax'] = $row['points'];
	}
	
	if(!isset($drivers[$row['user_id']]))
	{
		$d = new driver($row['user_id'], $row['user_drivername']);
		$d->addWeight(calculateWeight($division, $row['points'], 'champ', $champ_weight));
		$drivers[$row['user_id']] = $d;
	}
	else
	{
		$drivers[$row['user_id']]->addWeight(calculateWeight($division, $row['points'], 'champ', $champ_weight));
	}
}

echo '<br>';

if($_GET['lastevent'] >= 124)
{
    //Division 1 Event Penalties
    $q = 'SELECT (SELECT SUM(r_points) FROM lc_raceresults, lc_eventsignups WHERE es_driver = r_driver AND es_division = ' . $division['id'] . ' 
				AND r_event = es_event AND r_driver = u.user_id AND r_event = '.$event.') as points,
      			pf_drivername as user_drivername, u.user_id
    		FROM lc_champtable_drivers, bb_users u, bb_profile_fields_data d
    		WHERE u.user_id = d.user_id AND u.user_id = ct_id AND ct_div = ' . $division['id'] . '
    		ORDER BY points DESC
    		LIMIT 0, ' . $event_count;
        
    $res = $db->sql_query($q);
    while ($row = $db->sql_fetchrow($res))
    {
    	echo $row['user_drivername'] . ' ' . $row['points'] . '<br>';
    	if(!isset($division['eventmax']))
    	{
    		$division['eventmax'] = $row['points'];
    	}
    	
    	if(!isset($drivers[$row['user_id']]))
    	{
    		$d = new driver($row['user_id'], $row['user_drivername']);
    		$d->addWeight(calculateWeight($division, $row['points'], 'event', $event_weight));
    		$drivers[$row['user_id']] = $d;
    	}
    	else
    	{
    		$drivers[$row['user_id']]->addWeight(calculateWeight($division, $row['points'], 'event', $event_weight));
    	}
    }
	echo '<br>';
}

if($_GET['lastevent'] >= 125)
{
    //Division 1 Event Penalties
    $q = 'SELECT pf_drivername as user_drivername, u.user_id
    		FROM lc_champsignups, bb_users u, bb_profile_fields_data d
    		WHERE u.user_id = d.user_id AND u.user_id = cs_driver AND cs_div = ' . $division['id'] . ' AND
          (SELECT COUNT(*) FROM lc_raceresults, lc_events WHERE r_driver = u.user_id AND r_event = e_id AND e_championship = cs_champid) == 0';
    $res = $db->sql_query($q);
    while ($row = $db->sql_fetchrow($res))
    {
    	echo $row['user_drivername'] . ' Late Entrant Penalty ' . $new_weight . 'kg<br>';
        
        //overwrite all penalties
    	$d = new driver($row['user_id'], $row['user_drivername']);
    	$d->addWeight($new_weight);
    	$drivers[$row['user_id']] = $d;

    }

    echo '<br>';
}

usort($drivers, "compare_drivers");


foreach($drivers as $d)
{
	if($d->getWeight() > 0)
	{
		echo $d->getOutput() . '<br>';
	}
}

?>